﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

public class KanbanConfig
{
    private XmlNode _configNode;

    public KanbanConfig(XmlNode configNode)
    {
        _configNode = configNode;
    }

    private KanbanConfig()
    {
    }

    public string TfsServerUri
    {
        get { return _configNode.Attributes["TfsServerUri"].Value; }
    }

    public string TfsCollectionName
    {
        get { return _configNode.Attributes["TfsCollectionName"].Value; }
    }

    public string TfsProjectName
    {
        get { return _configNode.Attributes["TfsProjectName"].Value; }
    }

    public string BlockedField
    {
        get
        {
            if (_configNode.Attributes["BlockedField"] != null)
                return _configNode.Attributes["BlockedField"].Value;
            else
                return null;
    	}
    }

    public IEnumerable<WorkItemConfig> WorkItemConfigs
    {
        get
        {
            return (from XmlNode node in _configNode.SelectNodes("WorkItemTypes/WorkItemType")
                    select new WorkItemConfig(node.Attributes["Name"].Value, node.Attributes["CssClass"].Value));
        }
    }

    public IEnumerable<LaneDefinition> LaneDefinitions
    {
        get
        {
            return (from XmlNode laneDefinition in _configNode.SelectNodes("LaneDefinitions/LaneDefinition")
                    select new LaneDefinition(laneDefinition));
        }
    }

    public IEnumerable<AreaInfo> AreaInfos
    {
        get
        {
            return (from XmlNode areaInfo in _configNode.SelectNodes("WorkItemTypes/AreaPaths/Area")
                    select new AreaInfo(areaInfo));
        }
    }

    public string[] ChildStates
    {
        get
        {
            return (from XmlNode state in _configNode.SelectNodes("WorkItemTypes/ChildStates/State")
                    select state.Attributes["Name"].Value).ToArray();
        }
    }

    public string CumulativeFlowUri
    {
        get
        {
            XmlNode report = _configNode.SelectSingleNode("Reports/Report[@Name='Cumulative Flow']");
            if (report == null)
                return string.Empty;
            else
                return report.Attributes["Uri"].Value;
        }
    }
}